<!-- 产品 -->
<template>
  <div class="app-container">
    <!-- 检索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品系列" prop="type">
        <!-- <el-input
          v-model="queryParams.type"
          placeholder="请输入产品系列"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          v-model="queryParams.seriesId"
          placeholder="请选择产品系列"
          :options="allProductSeries"
          :props="cascaderProps"
          clearable
          filterable
        ></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 操作项 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addSeries"
          v-hasPermi="['system:role:add']"
        >
          新增系列
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleProject('add')"
          v-hasPermi="['system:role:add']"
        >
          新增产品
        </el-button>
      </el-col>
    </el-row>

    <!-- 内容 -->
    <el-table class="table_box" v-loading="loading" :data="tableList">
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="产品名称" prop="name" align="center" />
      <el-table-column label="产品图片" prop="picUrl" align="center">
        <template slot-scope="scope">
          <img
            class="table_pic_box"
            :src="scope.row.picUrl.split(',')[0]"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column label="产品系列" prop="seriesName" align="center" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProject('edit', scope.row)"
            v-hasPermi="['system:role:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleProject('detail', scope.row)"
            v-hasPermi="['system:role:edit']"
          >
            详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      @pagination="getList"
    />

    <!-- 新增系列 -->
    <SeriesDialog ref="seriesDialogRef" />

    <!-- 新增产品 -->
    <AddProductDialog ref="addProductDialogRef" @updateTable="resetQuery" />
  </div>
</template>
<script>
import SeriesDialog from "./components/seriesDialog.vue";
import AddProductDialog from "./components/addProductDialog.vue";
import {
  productList,
  productDelete,
  productSeriesList,
} from "@/api/useMenu/product";
export default {
  name: "Product",
  components: { SeriesDialog, AddProductDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        seriesId: 0,
        name: undefined,
        type: undefined,
      },
      cascaderProps: {
        emitPath: false,
        value: "id",
        label: "name",
        children: "seriesList",
      },
      allProductSeries: [], // 系列列表
    };
  },
  async created() {
    await this.getAllProductSeries();
    this.getList();
  },
  mounted() {},
  methods: {
    /** 查询表格数据 */
    async getList() {
      this.loading = true;
      let response = await productList(this.queryParams);
      this.tableList = response.rows;
      this.total = response.total;
      this.loading = false;
    },
    async getAllProductSeries() {
      // let res = await productSeriesAllList();
      let { rows } = await productSeriesList({
        pageNum: 1,
        pageSize: 9999,
        level: 1,
      });
      this.allProductSeries = [...rows];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增产品
    handleProject(enterType = "add", item = {}) {
      this.$refs.addProductDialogRef.openDialog(enterType, item);
    },
    // 新增系列
    addSeries() {
      // this.$refs.seriesDialogRef.openDialog();
      this.$router.push("/project/product/series");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id, name } = row;
      this.$modal
        .confirm('是否确认删除标题为"' + name + '"的数据项？')
        .then(function () {
          return productDelete(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .table_box {
  .table_pic_box {
    height: 80px;
    // width: 100%;
    // height: 100%;
    // max-width: 450px;
    // max-height: 120px;
  }
}
</style>
